package com.china.zhuoyue.ui.activity.market

import android.graphics.Paint
import androidx.recyclerview.widget.GridLayoutManager
import com.china.zhuoyue.R
import com.china.zhuoyue.bean.IndexFun
import com.china.zhuoyue.bean.IndexService
import com.china.zhuoyue.databinding.ActivityMarketMainBinding
import com.china.zhuoyue.databinding.ItemMarketFunBinding
import com.china.zhuoyue.databinding.ItemMarketServiceBinding
import com.china.zhuoyue.ui.base.BaseActivity
import com.china.zhuoyue.ui.base.BaseRecyclerAdapter
import com.china.zhuoyue.ui.base.BaseViewHolder
import com.china.zhuoyue.ui.ext.format
import com.china.zhuoyue.ui.ext.getString
import com.china.zhuoyue.ui.ext.tabData
import com.china.zhuoyue.ui.ext.toColor
import com.china.zhuoyue.ui.utils.FullyGridLayoutManager
import com.china.zhuoyue.ui.utils.GlideImageLoader
import com.yanzhenjie.sofia.Utils
import com.youth.banner.BannerConfig

class MarketMainActivity : BaseActivity<ActivityMarketMainBinding>() {
    override fun initView() {
//        Utils.setStatusBarColor(this, R.color.main_bg.toColor())
        loadView {
            tabLayout.tabData {
                tab(R.string.text_market_nav_1.getString(), R.mipmap.icon_market_my_push, R.mipmap.icon_market_my_push)
                tab(R.string.text_market_nav_2.getString(), R.mipmap.icon_market_my_need_push, R.mipmap.icon_market_my_need_push)
                tab(R.string.text_market_nav_3.getString(), R.mipmap.icon_market_collection, R.mipmap.icon_market_collection)
            }
            val cover = mutableListOf<Int>().apply {
                add(R.mipmap.banner_market_1)
                add(R.mipmap.banner_market_2)
                add(R.mipmap.banner_market_3)
            }
            banner.run {
                setImageLoader(GlideImageLoader())
                setIndicatorGravity(BannerConfig.RIGHT)
                setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                setDelayTime(2000)
                setImages(cover)
            }.start()

            rvFun.adapter = MarketFunAdapter().apply {
                datas = MarketFunAdapter.MARKET_FUN
            }
            rvFun.layoutManager = FullyGridLayoutManager(mThis, 5)

            rvGoods.adapter = MarketServiceAdapter().apply {
                datas = MarketServiceAdapter.MARKET_SERVICE
            }
            rvGoods.layoutManager = GridLayoutManager(mThis, 2)
        }
    }
}

class MarketFunAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMarketFunBinding>, IndexFun>() {
    companion object {
        val MARKET_FUN = mutableListOf<IndexFun>().apply {
            add(IndexFun(R.string.text_market_fun_1, R.mipmap.icon_market_fun_1))
            add(IndexFun(R.string.text_market_fun_2, R.mipmap.icon_market_fun_2))
            add(IndexFun(R.string.text_market_fun_3, R.mipmap.icon_market_fun_3))
            add(IndexFun(R.string.text_market_fun_4, R.mipmap.icon_market_fun_4))
            add(IndexFun(R.string.text_market_fun_5, R.mipmap.icon_market_fun_5))
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<ItemMarketFunBinding>, bean: IndexFun, position: Int) {
        holder.vb.apply {
            ivImage.setImageResource(bean.iconResId)
            tvName.setText(bean.nameResId)
        }
    }
}

data class MarketService(
    val imageResId: Int,
    val title: String,
    val price: Double,
    val marketPrice: Double,
    val loc: String,
)

class MarketServiceAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMarketServiceBinding>, MarketService>() {
    companion object {
        val MARKET_SERVICE = mutableListOf<MarketService>().apply {
            add(MarketService(R.mipmap.icon_market_1, "红色iPhone11 64GB全新", 4199.0, 4699.0, "深圳市龙华新区"))
            add(MarketService(R.mipmap.icon_market_2, "蓝色iPhone11 128GB全新", 4599.0, 4999.0, "深圳市龙华新区"))
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<ItemMarketServiceBinding>, bean: MarketService, position: Int) {
        holder.vb.apply {
            bean.apply {
                ivImage.setImageResource(imageResId)
                tvTitle.text = title
                tvPrice.text = getString(R.string.text_rmb, price.format())
                tvMarketPrice.text = getString(R.string.text_rmb, marketPrice.format())
                tvMarketPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                tvLoc.text = loc
            }
        }
    }
}